#!/bin/bash

#                                #
# Curl would be the alternative! # 
#                                #

loop=1
cnt=""
wan_old=""
wan_new=""
wan=""
while [ $loop != 0  ]; do

	wget --output-document=raw_file --output-file=getwan.log http://checkip.dyndns.org/
	#wan=$(cat raw_file)
	#wan=${wan:76:13}
	wan=$(grep -o "[0-9]\{1,3\}\..*\.[0-9]\{1,3\}" raw_file)
	wan_new=$wan
		
	#write wan to logfile	
	echo "$wan" >.temp_wan	

	# New IP received? # 
	if [ "$wan_new" != "$wan_old"  ]
	then
		echo "New WAN IP received!\n" > sendmail.log
		echo "WAN IP IS $wan_new"     >> sendmail.log
		echo "Starting mail-telnet-client" >> sendmail.log
		#sleep 1
		./sendmail.sh $wan_new >> sendmail.log
		cnt=`expr $cnt + 1`
		echo "MAIL $cnt was send at $(date)." >> sendmail.log
		sleep 3600
	fi

	wan_old=$wan
done
